<?php
  require_once '../../Helpers/DbHelper.php';
  require_once '../../Controllers/AdminController.php';
  require_once '../../Controllers/SchoolController.php';
  require_once '../../Controllers/UserController.php';
  require_once '../../Helpers/PathHelper.php';

  $u = new UserController();
  $u->evaluatePagePermission(UserController::ADMINISTRATOR);
  //
  $s = new SchoolController();
  $departmentList = $s->getDepartmentList();
  //
  $a = new AdminController();
  $yearLevelList = $a->getYearLevel();

  if(isset($_POST) && isset($_POST['add-fees']))
  {
    $feeDesc = $_POST['fee-desc'];
    $feeAmount = $_POST['fee-amount'];
    $yearId = $_POST['year'];

    if(!empty($feeAmount) && !empty($feeDesc))
     $a->addFees($feeDesc,$feeAmount,$yearId);
    //
    $feesList = $a->getFeesList();
  }
 else if(isset($_GET['action']) && !empty($_POST))
  {
    //delete selected fees
    $ids = $_POST['feeId'];
    //
    for($i=0;$i<count($ids);$i++)
      $a->removeFees($ids[$i]);
    //show updated fees table
    $feesList = $a->getFeesList();
  }
  else
  {
    $feesList = $a->getFeesList();
  }
?>
<html>
  <head>
    <title>School Enrollment :: School Fees</title>
    <?php
      require_once '../commons.php';
      renderHeaderStyles() ;
    ?>  </head>
    <div class="admin-container">
         <?php renderAdminNav(); ?>
    </div>
  <div class="container">
    <h1>Add Fees</h1>
    <form name="fees-form" method="post">
      <table align="center" class="labels">
        <tr>
          <td><label for="fee-desc">Fee Description:</label></td>
          <td><input type="text" name="fee-desc" /></td>
        </tr>
        <tr>
          <td><label for="year-level">Choose a year level: </label></td>
          <td><select name="year">
            <?php
              if(mysql_num_rows($yearLevelList) < 1)
                echo "<option value='0'>No data available</option>";

              while($rows=mysql_fetch_assoc($yearLevelList))
              {
                $i = $rows['YearLevelId'];
                $n = $rows['YearLevelDescription'];
                echo "<option value='$i'>$n</option>";
              }
            ?>
          </select></td>
        </tr>
        <tr>
          <td><label for="fee-amount">Fee Amount (PHP):</label></td>
          <td><input type="text" name="fee-amount" /><br /></td>
        </tr>
        <tr>
          <td><input type="submit" name="add-fees" value="Add Fees" /></td>
        </tr>
      </table>
    </form>
    <hr />
      <form name="fees-list" method="post" action="fees.php?action=delete">
      <table cellspacing="5" align="center" class="table">
      <tr>
        <th>&nbsp;</th>
        <th width="150">Year Level</th>
        <th>Fee Description</th>
        <th>Amount (PHP)</th>
        <th>Operations</th>
      </tr>
      <?php
        if(mysql_num_rows($feesList) < 1)
          echo "<tr><td colspan='3'>No data available.</td></tr>";
        while($rows = mysql_fetch_assoc($feesList))
        {
          $yearLevel = $rows['YearLevelDescription'];
          $feesName = $rows['FeeName'];
          $feeAmount = $rows['FeeAmount'];
          $feeId = $rows['FeeId'];


          echo "<tr>
            <td><input type='checkbox' name='feeId[]' value='$feeId' /></td>
            <td>$yearLevel</td>
            <td>$feesName</td>
            <td>$feeAmount</td>
            <td><a href='edit_fees.php?fid=$feeId'>Edit</a></td>
          </tr>";
        }
        echo "<tr><td><input type='submit' name='delete-fees' value='Delete Selected' />";
        echo "</tr>";
      ?>
    </table>
  </div>
</html>